package com.dfbz.controller.meet;

import com.dfbz.controller.BaseServlet;
import com.dfbz.model.Meeting;
import com.dfbz.model.User;
import com.dfbz.pojo.PageEntity;
import com.dfbz.service.MeetingService;
import com.dfbz.service.impl.MeetingServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/meet/*")
public class MeetServlet extends BaseServlet {
    MeetingService meetingService=new MeetingServiceImpl();
    public void meeting(HttpServletRequest req, HttpServletResponse resp){
        try {
            HttpSession session = req.getSession();
            User userInfo =(User) session.getAttribute("userInfo");
            String p=req.getParameter("meetCurrPage");
            String meeting_title=req.getParameter("meeting_title");
            String status2=req.getParameter("status");
            Integer status=-1;
            Integer currPage;
            if (p==null){
                currPage=1;
            }else {
                currPage=Integer.parseInt(p);
            }
            if (meeting_title==null){
                if (null != session.getAttribute("meeting_title")){
                    meeting_title=(String) session.getAttribute("meeting_title");
                }
                else {
                    meeting_title="";
                }
            }
            if (status2==null){
                if (null !=session.getAttribute("status")){
                    status=(Integer)session.getAttribute("status");
                }
                else {
                    status=-1;
                }
            }
            else if (status2.equals("未开始")){
                status=0;
            }
            else if (status2.equals("正在进行")){
                status=1;
            }
            else if (status2.equals("已结束")){
                status=2;
            }
            else if (status2.equals("请选择状态")){
                status=-1;
            }
            session.setAttribute("status",status);
            session.setAttribute("meeting_title",meeting_title);
            PageEntity<Meeting> MeetPageEntity = meetingService.findPage(currPage, meeting_title, status);
            session.setAttribute("MeetPageEntity",MeetPageEntity);
            req.getRequestDispatcher("/view/location/meeting").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void next(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        PageEntity<Meeting> meetPageEntity = (PageEntity<Meeting>) session.getAttribute("MeetPageEntity");
        Integer status = (Integer) session.getAttribute("status");
        String meeting_title = (String) session.getAttribute("meeting_title");
        Integer currPage = meetPageEntity.getCurrPage();
        Integer end = meetPageEntity.getEnd();
        Integer totalPage = meetPageEntity.getTotalPage();
        try {
            if (currPage==end){
                if (currPage==totalPage){
                    req.getRequestDispatcher("/view/location/meeting").forward(req,resp);
                    return;
                }
                else {
                    currPage+=1;
                    PageEntity<Meeting> PageEntity = meetingService.findPage(currPage, meeting_title, status);
                    session.setAttribute("MeetPageEntity",PageEntity);
                    req.getRequestDispatcher("/view/location/meeting").forward(req,resp);
                    return;
                }
            }
            else {
                currPage+=1;
                PageEntity<Meeting> PageEntity = meetingService.findPage(currPage, meeting_title, status);
                session.setAttribute("MeetPageEntity",PageEntity);
                req.getRequestDispatcher("/view/location/meeting").forward(req,resp);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void back(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        PageEntity<Meeting> meetPageEntity = (PageEntity<Meeting>) session.getAttribute("MeetPageEntity");
        Integer status = (Integer) session.getAttribute("status");
        String meeting_title = (String) session.getAttribute("meeting_title");
        Integer currPage = meetPageEntity.getCurrPage();
        Integer begin = meetPageEntity.getBegin();
        try {
            if (currPage==begin){
                if (currPage==1){
                    req.getRequestDispatcher("/view/location/meeting").forward(req,resp);
                    return;
                }
                else {
                    currPage-=1;
                    PageEntity<Meeting> PageEntity = meetingService.findPage(currPage, meeting_title, status);
                    session.setAttribute("MeetPageEntity",PageEntity);
                    req.getRequestDispatcher("/view/location/meeting").forward(req,resp);
                    return;
                }
            }
            else {
                currPage-=1;
                PageEntity<Meeting> PageEntity = meetingService.findPage(currPage, meeting_title, status);
                session.setAttribute("MeetPageEntity",PageEntity);
                req.getRequestDispatcher("/view/location/meeting").forward(req,resp);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
